package com.sinodata.bsm.center.service.event;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.sinodata.bsm.center.dao.BaseDao;
import com.sinodata.bsm.center.dao.impl.EventCorrelationDaoImpl;
import com.sinodata.bsm.common.vo.EventCorrelation;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author liulibo
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-6-13 PM 2:19:28     liulibo        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
@Service
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public class EventCorrelationService {

    //    private static final Logger logger = Logger.getLogger(EventCorrelationService.class);

    @Autowired
    private BaseDao baseDao;

    @Autowired
    private EventCorrelationDaoImpl eventCorrelationDao;

    /**
     * 增加事件关联记录
     * 
     * @param eventCorrelation
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void add(EventCorrelation eventCorrelation) {
        if (eventCorrelation.getMainEventId().equals(eventCorrelation.getEventId())) {
            return;
        }
        //过滤重复记录、主事件ID、从事件ID 存在即认为记录存在
        List<EventCorrelation> temp = findByMainAndSubEvent(eventCorrelation.getMainEventId(), eventCorrelation.getEventId());
        if (temp != null && temp.size() > 0) {
            return;
        }
        //TODO 避免首尾相连，入库的时候控制，还是显示的时候控制

        baseDao.save(eventCorrelation);
    }

    /**
     * 通过主事件ID，从事件ID查询事件关联集合
     * 
     * @param mainEventId
     * @param subEventId
     * @return
     */
    private List<EventCorrelation> findByMainAndSubEvent(Long mainEventId, Long subEventId) {
        return eventCorrelationDao.findByMainAndSubEvent(mainEventId, subEventId);
    }

    /**
     * 通过主事件ID查询从事件，递归查询遍历全部从事件
     * 
     * @param mainEventId
     * @return
     */
    public List<EventCorrelation> findByMainEvent(Long mainEventId) {
        return eventCorrelationDao.findByMainEvent(mainEventId);
    }

}